//
//  DeviceViewController.m
//  LightControl
//
//  Created by CKF on 2025/8/12.
//

#import "DeviceViewController.h"
#import "CustomArcView.h"
#import "CustomSlider.h"
#import "MenuWindowView.h"
@interface DeviceViewController () 
@property(nonatomic,strong)UIScrollView *scrollview;
@property(nonatomic,strong)CustomArcView *arcView;
@property(nonatomic,strong)CustomSlider *customSlider;
@property(nonatomic,strong)UIButton *onButton;
@property(nonatomic,strong)UIButton *offBtn;
@property(nonatomic,strong)UIButton *ecoButton;
@property(nonatomic,strong)UIButton *turboBtn;
@property(nonatomic,strong)UIButton *smartBtn;
@property(nonatomic,strong)UILabel *ecoLabel;
@property(nonatomic,strong)UILabel *turboLabel;
@property(nonatomic,strong)UILabel *smartLabel;
@property(nonatomic,strong)UILabel *infoLabel;
@property(nonatomic,strong)NSMutableArray<UIView *> *dotViews;
@property(nonatomic,strong)MenuWindowView *menu;
@property(nonatomic,strong)UITapGestureRecognizer *tap;
@end

@implementation DeviceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"00:11:22:33:44:55";
    [self initSubViews];
    [self addRightItem];
}

-(void)addRightItem{
    UIButton * rightBtn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    [rightBtn setImage:[UIImage imageNamed:@"DisplaySettings"] forState:0];
    [rightBtn addTarget:self action:@selector(rightAct) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
}

-(void)rightAct{
    if (!_menu) {
        _menu = [[MenuWindowView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight-mNavBarHeight)];
        [self.view addSubview:_menu];
    }
    _menu.hidden = !_menu.hidden;
    if (_menu.hidden) {
        [_menu hide];
    }else{
        [_menu display];
    }
}

- (void)selectCellImplementAtIndexRow:(NSInteger)rowNumber;

{
    if(rowNumber == 0)
    {
        NSLog(@"0");

    }
    if(rowNumber == 1)
    {
        NSLog(@"1");
    }
    if(rowNumber == 2)
    {
        NSLog(@"2");
    }
    if(rowNumber == 3)
    {
        NSLog(@"3");
    }
    if(rowNumber == 4)
    {
        NSLog(@"4");
    }

}


-(void)initSubViews{
   
    [self.view addSubview:self.scrollview];
    
    [self.scrollview addSubview:self.arcView];
    
    [self.scrollview addSubview:self.customSlider];
    
    // 创建数值点
    [self createDotViews];
    
    // 更新滑块和点的样式
    [self updateSliderAndDots];
    
    [self.scrollview addSubview:self.onButton];
    [self.scrollview addSubview:self.offBtn];
    [self.scrollview addSubview:self.infoLabel];
    [self.scrollview addSubview:self.ecoButton];
    [self.scrollview addSubview:self.turboBtn];
    [self.scrollview addSubview:self.smartBtn];
    [self.scrollview addSubview:self.ecoLabel];
    [self.scrollview addSubview:self.turboLabel];
    [self.scrollview addSubview:self.smartLabel];
}

-(void)modeBtnAct:(UIButton *)btn{
    if (btn.tag == 1) {
        self.ecoButton.tintColor = RGB(0, 160, 239);
        self.turboBtn.tintColor = RGB(42, 87, 128);
        self.smartBtn.tintColor = RGB(42, 87, 128);
    }else  if (btn.tag == 2) {
        self.turboBtn.tintColor = RGB(0, 160, 239);
        self.ecoButton.tintColor = RGB(42, 87, 128);
        self.smartBtn.tintColor = RGB(42, 87, 128);
    }else  if (btn.tag == 3) {
        self.smartBtn.tintColor = RGB(0, 160, 239);
        self.turboBtn.tintColor = RGB(42, 87, 128);
        self.ecoButton.tintColor = RGB(42, 87, 128);
    }
}

-(void)onBtnAct{
    
}

-(void)offBtnAct{
    
}

// 2. 实现手势处理方法
- (void)sliderTapped:(UITapGestureRecognizer *)gesture {
    // 获取被点击的 UISlider
    UISlider *slider = (UISlider *)gesture.view;
    
    // 获取点击位置在 slider 坐标系中的坐标
    CGPoint point = [gesture locationInView:slider];
    
    // 计算点击位置占 slider 宽度的百分比
    CGFloat percent = point.x / slider.bounds.size.width;
    
    // 根据百分比计算新值
    float newValue = slider.minimumValue + (slider.maximumValue - slider.minimumValue) * percent;
    
    // 设置滑块值并启用动画
    [slider setValue:round(newValue) animated:YES];
    
    [self updateSliderAndDots];
}

- (void)createDotViews {
    float originX = 4;
    float width = self.customSlider.frame.size.width-14;
    for (int i = 0; i <= 6; i++) {
        CGFloat x = originX + i * (width / 6.0);
        UIView *dotView = [[UIView alloc] initWithFrame:CGRectMake(self.customSlider.frame.origin.x+x, self.customSlider.frame.origin.y + self.customSlider.frame.size.height / 2 - 1.5, 3, 3)];
        dotView.backgroundColor = RGB(167, 169, 174);
        dotView.layer.cornerRadius = 1.5;
        [self.scrollview addSubview:dotView];
        [self.dotViews addObject:dotView];
    }
}

- (void)sliderValueChanged:(UISlider *)slider {
    // 对滑块值进行取整
    slider.value = round(slider.value);
    [self updateSliderAndDots];
}

- (void)updateSliderAndDots {
    // 设置滑块轨迹颜色
    self.customSlider.minimumTrackTintColor = RGB(42, 87, 128);
    self.customSlider.maximumTrackTintColor = RGB(219, 221, 226);
    
    // 更新点的颜色
    NSInteger currentIndex = (NSInteger)self.customSlider.value;
    for (NSInteger i = 0; i < self.dotViews.count; i++) {
        UIView *dotView = self.dotViews[i];
        if (currentIndex == i) {
            dotView.backgroundColor = [UIColor clearColor];
        } else {
            dotView.backgroundColor = RGB(167, 169, 174);
        }
    }
   
}

-(UIButton *)onButton{
    if (!_onButton) {
        _onButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _onButton.frame = CGRectMake(KScreenWidth*0.5-86, CGRectGetMaxY(self.customSlider.frame)+60, 80, 40);
        [_onButton setBackgroundColor:RGB(42, 87, 128)];
        [_onButton setTitle:LocalString(@"on") forState:0];
        _onButton.layer.cornerRadius = 20;
        _onButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [_onButton setTitleColor:RGB(217, 236, 241) forState:0];
        [_onButton addTarget:self action:@selector(onBtnAct) forControlEvents:UIControlEventTouchUpInside];
    }
    return _onButton;
}

-(UIButton *)offBtn{
    if (!_offBtn) {
        _offBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _offBtn.frame = CGRectMake(KScreenWidth*0.5+6, CGRectGetMaxY(self.customSlider.frame)+60, 80, 40);
        [_offBtn setBackgroundColor:RGB(235, 235, 235)];
        [_offBtn setTitle:LocalString(@"off") forState:0];
        _offBtn.layer.cornerRadius = 20;
        _offBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [_offBtn setTitleColor:RGB(22, 23, 27) forState:0];
        [_offBtn addTarget:self action:@selector(offBtnAct) forControlEvents:UIControlEventTouchUpInside];
    }
    return _offBtn;
}

-(UILabel *)infoLabel{
    if (!_infoLabel) {
        _infoLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, CGRectGetMaxY(self.offBtn.frame)+30, KScreenWidth-160, 26)];
        _infoLabel.font = [UIFont systemFontOfSize:17];
        _infoLabel.textColor = RGB(22, 23, 27);
        _infoLabel.text = LocalString(@"Fan mode");
        _infoLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _infoLabel;
}

-(UIButton *)ecoButton{
    if (!_ecoButton) {
        _ecoButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _ecoButton.frame = CGRectMake(KScreenWidth*0.5-120, CGRectGetMaxY(self.infoLabel.frame)+20, 44, 44);
        UIImage *image = [UIImage imageNamed:@"Eco"];
        UIImage *templateImage = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        [_ecoButton setImage:templateImage forState:0];
        _ecoButton.tintColor = RGB(42, 87, 128);
        _ecoButton.tag = 1;
        [_ecoButton addTarget:self action:@selector(modeBtnAct:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _ecoButton;
}

-(UIButton *)turboBtn{
    if (!_turboBtn) {
        _turboBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _turboBtn.frame = CGRectMake(KScreenWidth*0.5-22,CGRectGetMaxY(self.infoLabel.frame)+20, 44, 44);
        UIImage *image = [UIImage imageNamed:@"Turbo"];
        UIImage *lImage = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        [_turboBtn setImage:lImage forState:0];
        _turboBtn.tintColor = RGB(42, 87, 128);
        _turboBtn.tag = 2;
        [_turboBtn addTarget:self action:@selector(modeBtnAct:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _turboBtn;
}

-(UIButton *)smartBtn{
    if (!_smartBtn) {
        _smartBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _smartBtn.frame = CGRectMake(KScreenWidth*0.5+76, CGRectGetMaxY(self.infoLabel.frame)+20, 44, 44);
        UIImage *image = [UIImage imageNamed:@"Smart"];
        UIImage *lImage = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        [_smartBtn setImage:lImage forState:0];
        _smartBtn.tintColor = RGB(42, 87, 128);
        _smartBtn.tag = 3;
        [_smartBtn addTarget:self action:@selector(modeBtnAct:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _smartBtn;
}



-(UILabel *)ecoLabel{
    if (!_ecoLabel) {
        _ecoLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMidX(self.ecoButton.frame)-30, CGRectGetMidY(self.ecoButton.frame)+25, 60, 20)];
        _ecoLabel.font = [UIFont systemFontOfSize:14];
        _ecoLabel.textColor = RGB(22, 23, 27);
        _ecoLabel.text = LocalString(@"ECO");
        _ecoLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _ecoLabel;
}

-(UILabel *)turboLabel{
    if (!_turboLabel) {
        _turboLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMidX(self.turboBtn.frame)-30, CGRectGetMidY(self.turboBtn.frame)+25, 60, 20)];
        _turboLabel.font = [UIFont systemFontOfSize:14];
        _turboLabel.textColor = RGB(22, 23, 27);
        _turboLabel.text = LocalString(@"TURBO");
        _turboLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _turboLabel;
}

-(UILabel *)smartLabel{
    if (!_smartLabel) {
        _smartLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMidX(self.smartBtn.frame)-30, CGRectGetMidY(self.smartBtn.frame)+25, 60, 20)];
        _smartLabel.font = [UIFont systemFontOfSize:14];
        _smartLabel.textColor = RGB(22, 23, 27);
        _smartLabel.text = LocalString(@"SMART");
        _smartLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _smartLabel;
}

-(CustomSlider *)customSlider{
    if (!_customSlider) {
        _customSlider = [[CustomSlider alloc] initWithFrame:CGRectMake(50, CGRectGetMaxY(_arcView.frame), self.view.bounds.size.width - 100, 20)];
        _customSlider.trackHeight = 6;
        _customSlider.minimumValue = 0;
        _customSlider.maximumValue = 6;
        _customSlider.thumbTintColor = RGB(42, 87, 128);
        _customSlider.value = 0;
        _customSlider.minimumTrackTintColor = RGB(42, 87, 128);
        _customSlider.maximumTrackTintColor =  RGB(219, 221, 226);
        [_customSlider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sliderTapped:)];
        [_customSlider addGestureRecognizer:tapGesture];
    }
    return _customSlider;
}

-(CustomArcView *)arcView{
    if (!_arcView) {
        _arcView = [[CustomArcView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenWidth)];\
        __weak typeof(self)weakSelf = self;
        _arcView.callBack = ^(int level) {
            [weakSelf.customSlider setValue:level animated:YES];
            [weakSelf updateSliderAndDots];
        };
    }
    return _arcView;
}

-(UIScrollView *)scrollview{
    if (!_scrollview) {
        _scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight-mNavBarHeight)];
        _scrollview.backgroundColor = RGB(242, 245, 249);
    }
    return _scrollview;
}



-(NSMutableArray *)dotViews{
    if (!_dotViews) {
        _dotViews = [[NSMutableArray alloc]init];
    }
    return _dotViews;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
